/**
 * Title:        流程编号检测（后台设置）
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.FontFileProcessing;
import weaver.general.GCONST;
import weaver.general.OrderProperties;
import weaver.general.Util;



public class WorkflowCodeCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowCodeCheck  start");

        String excSql = " select wfc.id as codeid,wfc.codevalue,wfb.workflowname from workflow_codeRegulate wfc ";
        excSql += " inner join workflow_base wfb on wfc.workflowid = wfb.id ";
        excSql += " where wfc.workflowid in ";
        excSql += " (select workflowid from ";
        excSql += " (select count(1) as cnt,workflowid from workflow_codeRegulate  ";
        excSql += " where concretefield = 8 and workflowid > 0 ";
        excSql += " group by workflowid) aaa where cnt > 1) ";
        excSql += " and wfc.concretefield = 8 order by wfb.id ";
        recordSet.executeSql(excSql);
        int cnt = 0;
        List resultList = new ArrayList();
        String result = "";
        while(recordSet.next()){
            String codeRegulateid = recordSet.getString("codeid");
            String codevalue = recordSet.getString("codevalue");
            String workflowname = recordSet.getString("workflowname");
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage",formatMessage("流程[" + workflowname + "]", "存在多个流水号位数。", "位数=" + codevalue));
            resultMap.put("codeRegulateid",codeRegulateid);
            resultMap.put("buttonname","删除");
            resultMap.put("fixtype","1");
            resultList.add(resultMap);
            cnt++;
        }
        excSql = "";
        excSql += "  select wfc.id as codeid,wfc.codevalue,htmllabelinfo.labelname from workflow_codeRegulate wfc  ";
        excSql += "  inner join workflow_bill wfb on wfc.formid = wfb.id  ";
        excSql += "  left join htmllabelinfo on htmllabelinfo.indexid = wfb.namelabel and htmllabelinfo.languageid = 7  ";
        excSql += "  where wfc.formid in(  ";
        excSql += "  select formid from(select count(1) as cnt, workflow_codeRegulate.formid  ";
        excSql += "    from workflow_codeRegulate  ";
        excSql += "   inner join workflow_code  ";
        excSql += "      on workflow_codeRegulate.workflowid = workflow_code.flowid and workflow_codeRegulate.formid = workflow_code.formid ";
        excSql += "     and (workflowseqalone != '1' or workflowseqalone is null)  ";
        excSql += "   where concretefield = 8  ";
        excSql += " and workflow_codeRegulate.formid  < -1 ";
        excSql += "     and workflow_codeRegulate.formid != -1  ";
        excSql += "   group by workflow_codeRegulate.formid  ";
        excSql += "   )aaa  ";
        excSql += "    where aaa.cnt > 1)  ";
        excSql += "  and wfc.concretefield = 8 order by wfb.id  ";
        recordSet.executeSql(excSql);
        while(recordSet.next()){
            String codeRegulateid = recordSet.getString("codeid");
            String codevalue = recordSet.getString("codevalue");
            String fromname = recordSet.getString("labelname");
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage",formatMessage("表单[" + fromname + "]", "存在多个流水号位数。", "位数=" + codevalue));
            resultMap.put("codeRegulateid",codeRegulateid);
            resultMap.put("buttonname","删除");
            resultMap.put("fixtype","2");
            resultList.add(resultMap);
            cnt++;
        }
        if(cnt == 0){
            result = resultOk;
            resultList.add("没有存在多个流水号位数的数据。");
        }
        (new BaseBean()).writeLog("WorkflowCodeCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        setRecordSet();
        (new BaseBean()).writeLog("WorkflowCodeCheck  start");

        List resultList = new ArrayList();
        String result = resultOk;
        if(dataMap.get("codeRegulateid") != null && !dataMap.get("codeRegulateid").equals("")){
            //根据参数获取列表
            String codeRegulateids = (String)dataMap.get("codeRegulateid");
            String divids =  (String)dataMap.get("divid");
            String fixtype =  (String)dataMap.get("fixtype");
            List<String> codeRegulateidList = Util.splitString2List(codeRegulateids, ",");
            List<String> dividList = Util.splitString2List(divids, ",");
            for(int i = 0; i < codeRegulateidList.size(); i++){
                String codeRegulateid = codeRegulateidList.get(i);
                String divid = dividList.get(i);
                String sqlstr = " select count(1) as count from workflow_codeRegulate ";
                if(fixtype.equals("1")){
                    sqlstr += "where concretefield = 8 and workflowid = (select workflowid from workflow_codeRegulate where id =" + codeRegulateid + " )";
                }else{
                    sqlstr += "where concretefield = 8 and formid = (select formid from workflow_codeRegulate where id =" + codeRegulateid + " )";
                }
                
                recordSet.executeSql(sqlstr);
                Map resultMap = new HashMap<String,String>();
                if(recordSet.next()){
                    if(recordSet.getInt("count") > 1){
                        recordSet.executeUpdate(" delete from workflow_codeRegulate where id =" + codeRegulateid);
                        resultMap.put("result",resultOk);
                        resultMap.put("divid",divid);
                        resultMap.put("fixMessage","修复成功");
                    }else{
                        resultMap.put("result",resultOk);
                        resultMap.put("divid",divid);
                        if(fixtype.equals("1")){
                            resultMap.put("fixMessage","该流程只存在一个流水号位数，不需要修复");
                        }else{
                            resultMap.put("fixMessage","该表单只存在一个流水号位数，不需要修复");
                        }
                    }
                }
                resultList.add(resultMap);
                
            }
            //return formatReturnJsonFix(result, resultList,this);
        }
        needReload = "1";
        return checkData(new HashMap());
    }

    @Override
    public String getClassName() {
        return "流程编号检测（后台设置）";
    }

    @Override
    public String getClassDesc() {
        return "检测流程编号后台设置中是否存在多个流水号位数";
    }

}



